package ejb.cbm.operatoria.session;

import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ejb.cbm.operatoria.entity.FormatoImpresionEntity;
import ejb.cbm.operatoria.local.FormatoImpresionSessionLocal;

////@Stateless
public class FormatoImpresionSession extends HibernateDaoSupport implements FormatoImpresionSessionLocal {
	
	static Logger logger = Logger.getLogger(CuentaSession.class.getName());
	//property constants
	public static final String CABECERA = "cabecera";
	public static final String CUERPO = "cuerpo";
	public static final String GLOSA = "glosa";
	public static final String USUARIOREGISTRO = "usuarioregistro";
	public static final String USUARIOMODIFICACION = "usuariomodificacion";


    //@PersistenceContext 
	//private EntityManager entityManager;
	@Autowired
	public FormatoImpresionSession(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
    public void save(FormatoImpresionEntity transientInstance) {
    	logger.log("saving FormatoImpresionEntity instance", Level.INFO, null,null);
        try {
            getSession().persist(transientInstance);
            logger.log("save successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("save failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public void delete(FormatoImpresionEntity persistentInstance) {
    	logger.log("deleting FormatoImpresionEntity instance", Level.INFO, null,null);
        try {
            getSession().delete(persistentInstance);
            logger.log("delete successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("delete failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public FormatoImpresionEntity update(FormatoImpresionEntity detachedInstance) {
    	logger.log("updating FormatoImpresionEntity instance", Level.INFO, null,null);
        try {
            FormatoImpresionEntity result = (FormatoImpresionEntity) getSession().merge(detachedInstance);
            logger.log("update successful", Level.INFO, null,null);
            return result;
        } catch (RuntimeException re) {
        	logger.log("update failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public FormatoImpresionEntity findById( Long id) {
    	logger.log("finding FormatoImpresionEntity instance with id: " + id, Level.INFO, null,null);
        try {
            FormatoImpresionEntity instance = (FormatoImpresionEntity) getSession().get(FormatoImpresionEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
        	logger.log("find failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    //@SuppressWarnings("unchecked")
    public List<FormatoImpresionEntity> findByProperty(String propertyName, Object value) {
    	logger.log("finding FormatoImpresionEntity instance with property: " + propertyName
    						+ ", value: " + value, Level.INFO, null,null);
		try {
			String queryString = "select model from FormatoImpresionEntity model where model." 
			 						+ propertyName + "= :propertyValue";
			return getSession().createQuery(queryString)
 				.setParameter("propertyValue", value).list();
		} catch (RuntimeException re) {
			logger.log("find by property name failed", Level.FATAL,null, re);
			throw re;
		}
	}
	
	public List<FormatoImpresionEntity> findByCabecera(Object cabecera) {
		return findByProperty(CABECERA, cabecera);
	}
	
	public List<FormatoImpresionEntity> findByCuerpo(Object cuerpo) {
		return findByProperty(CUERPO, cuerpo);
	}
	
	public List<FormatoImpresionEntity> findByGlosa(Object glosa) {
		return findByProperty(GLOSA, glosa);
	}
	
	public List<FormatoImpresionEntity> findByUsuarioregistro(Object usuarioregistro) {
		return findByProperty(USUARIOREGISTRO, usuarioregistro);
	}
	
	public List<FormatoImpresionEntity> findByUsuariomodificacion(Object usuariomodificacion) {
		return findByProperty(USUARIOMODIFICACION, usuariomodificacion);
	}
	

	//@SuppressWarnings("unchecked")
	public List<FormatoImpresionEntity> findAll() {
		logger.log("finding all FormatoImpresionEntity instances", Level.INFO, null,null);
		try {
			String queryString = "select model from FormatoImpresionEntity model";
			return getSession().createQuery(queryString).list();
		} catch (RuntimeException re) {
			logger.log("find all failed", Level.FATAL,null, re);
			throw re;
		}
	}
}